<?php
class HotelsController extends AppController {
    public $name    = 'Hotels';
    public $helpers = array('Form');
    public $scaffold;

    public function rest_list ($ext='xml') {
        $data = $this->Hotel->find('all', array('recursive'=>0));
        $this->rest_render($ext, $data);
    } // rest_list


    public function listing ($type=null) {
        $json = $this->Hotel->hotelList($this->params['form']);
        $this->response($json);
    } // listing

    public function save () {
        $this->data['Hotel'] = $this->params['form'];

        if (!$this->data['Hotel'] || count($this->data['Hotel']) == 0) { // check if data post
            $json = array(
                'status' => false,
                'mesg'   => __('no data posted', true)
            );

            $this->response($json);
            return;
        }

        foreach ($this->data['Hotel'] as $fieldname=>$value) { // convert empty data to null
            if (empty($value) && $value != '0') {
                $this->data['Hotel'][$fieldname] = null;
            }
        }

        $this->Hotel->set($this->data);

        if (!$this->Hotel->validates()) { // invalid data found!
            $json = array(
                'status' => false,
                'mesg'   => __('Please enter the details in the fields pointed out above and try again!', true)
            );

            foreach ($this->Hotel->invalidFields() as $k=>$v) {
                $json['error'][] = array(
                    'f' => $k,
                    'm' => $v
                );
            }

            $this->response($json);
            return;
        }

        if (!$this->Hotel->save($this->data)) { // error when saving data
            $mesg = empty($this->params['form']['id']) ? __('Fail insert record to database at this time, please try again.', true) : __('Fail update record to database at this time, please try again.', true);
            $err  = ($this->error) ? $this->error : $mesg;
            $json = array(
                'status' => false,
                'mesg'   => $err
            );

            $this->response($json);
            return;
        }

        $mesg = empty($this->params['form']['id']) ? __('New hotel has been created successfully!',true) : __('Hotel has been updated successfully!',true);
        $json = array(
            'status' => true,
            'mesg'   => $mesg
        );

        $this->response($json);
    } // save

    public function update () {
        $form = $this->params['form'];

        $this->Hotel->updateAll(
            array('Hotel.status' => '"D"'),
            array('Hotel.id' => explode(',', $form['id']))
        );

        if ($num = $this->Hotel->getAffectedRows()) {
            $json = array(
                'status'   => true,
                'affected' => $num);
        } else {
            $json = array('mesg' => __('Fail to update record at this time.', true));
        }


        $this->response($json);
    } // update
} // HotelsController
?>